﻿using System.Collections.Generic;
using System.Linq;
using WSU.PMP.BE.DATA.Abstract;
using WSU.PMP.BE.DATA.EF;

namespace WSU.PMP.BE.DATA.Concrete
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Item : IItem
    {
        public EfItem getItemByBarcode(string barcode)
        {       
            using (var db = new PmpEntities())
            {
                long key = long.Parse(barcode);

                EfItem item = (from i in db.EfItems
                               where i.Barcode == key
                               select i).FirstOrDefault();

                return item; 
            }
        }

        public EfItem getItemByName(string name)
        {
            using (var db = new PmpEntities())
            {
                EfItem item = (from i in db.EfItems
                               where i.Name == name
                               select i).FirstOrDefault();

                return item; 
            }
        }

        public List<EfItem> getItemsByName(string name)
        {
            using (var db = new PmpEntities())
            {
                IQueryable<EfItem> items = from i in db.EfItems
                                           where i.Name.Contains(name)
                                           select i;

                return items.ToList(); 
            }
        }

        public List<EfItem> getItemsByType(string type)
        {
            using (var db = new PmpEntities())
            {
                int key = int.Parse(type);

                IQueryable<EfItem> items = from i in db.EfItems
                                           where i.TypeID == key
                                           select i;

                return items.ToList(); 
            }
        }

        public List<EfItem> getItemsByCost(string min, string max)
        {
            using (var db = new PmpEntities())
            {
                int low = int.Parse(min);
                int high = int.Parse(max);

                IQueryable<EfItem> items = from i in db.EfItems
                                           where i.Cost >= low && i.Cost < high
                                           select i;

                return items.ToList(); 
            }
        }

        public void addItem(EfItem item)
        {
            using (var db = new PmpEntities())
            {
                db.EfItems.AddObject(item);
                db.SaveChanges(); 
            }
        }

        public void editItem(EfItem item)
        {
            using (var db = new PmpEntities())
            {
                db.EfItems.Attach(db.EfItems.Single(i => i.Barcode == item.Barcode));
                db.EfItems.ApplyCurrentValues(item);
                db.SaveChanges(); 
            }
        }

        public void deleteItem(EfItem item)
        {
            using (var db = new PmpEntities())
            {
                db.EfItems.Attach(item);
                db.EfItems.DeleteObject(item);
                db.SaveChanges(); 
            }
        }
    }
}
